package userlimitModel

import (
	"arabApi/app/api/models"
	"arabApi/pkg/database"
)

type UserRole struct {
	models.BaseModel
	UserId int64 `gorm:"column:user_id" db:"user_id"` //  用户id
	RoleId int64 `gorm:"column:role_id" db:"role_id"` //  角色id
	models.CommonTimestampsField
}

func (UserRole) TableName() string {
	return "tp_user_role"
}

func (UserRole) UserRoleAdd(id int64, um []UserRole) bool {

	tx := database.DB.Begin()

	ok := tx.Where("user_id = ?", id).Delete(&UserRole{})

	ok1 := tx.Model(&UserRole{}).Create(um)

	if ok != nil && ok1 != nil {
		tx.Commit()
		return true
	} else {
		tx.Rollback()
		return false
	}
}

//func Del(id int64) error {
//	var systemConfig1 int
//	ccc := database.DB.Where("id = ?", id).Update()
//	return ccc.Error
//}

func UserRole1(userId int64) ([]UserRole, int64) {

	var count int64
	dd := database.DB.Model(UserRole{})
	var users []UserRole
	dd.Count(&count).Where("user_id = ?", userId).Find(&users)
	return users, count

}

func UserRoleName(userId int64) []map[string]interface{} {

	var result []map[string]interface{}
	dd := database.DB.Table("tp_user_role").Debug().Select("tp_role.role_name").Joins("inner join tp_role on tp_user_role.role_id = tp_role.id")
	//var users []UserRole
	dd.Where("user_id = ?", userId).Find(&result)

	return result

}
